package com.cskaoyan.service;

import com.cskaoyan.bean.KeywordData;
import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Kongwo
 * @description
 * @since 2022/05/06 20:24
 */

@Service
@Transactional
public class KeywordServiceImpl implements KeywordService{

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public KeywordData getKeywordList(String keyword, String url, BaseParam param) {

        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();

        PageHelper.startPage(page, limit);
        List<MarketKeyword> list = marketKeywordMapper.selectKeywordList(keyword, url, sort, order);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return KeywordData.data(total, pages, limit, page, list);
    }

    @Override
    public MarketKeyword addNewKeyword(MarketKeyword marketKeyword) {

        marketKeywordMapper.insertSelective(marketKeyword);
        Integer id = marketKeyword.getId();
        return marketKeywordMapper.selectByPrimaryKey(id);
    }

    @Override
    public MarketKeyword updateKeyword(MarketKeyword marketKeyword) {

        marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        return marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
    }

    /**
     * 逻辑删除
     */
    @Override
    public void deleteKeyword(Integer id) {

        marketKeywordMapper.updateKeywordDeleted(id);
    }
}
